/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clienttest;

import clienttest.common.ConnectManager;
import clienttest.object.CreditCard;
import clienttest.object.ResultReponse;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

/**
 *
 * @author lekhanhduy
 */
public class ChangePassWord extends javax.swing.JFrame {

    private WindowAdapter windowAdapter;
    private ConnectManager connectManeger;
    private CreditCard creditCard;

    /**
     * Creates new form ChangePassWord
     */
    public ChangePassWord() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        connectManeger = new ConnectManager();
        windowAdapter = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent we) {
                setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            }
        };
        this.addWindowListener(windowAdapter);
        addCheckJpassWork(jPasswordFieldOdl);
        addCheckJpassWork(jPasswordFieldNew);
        addCheckJpassWork(jPasswordFieldNew1);
    }

    public void addCheckJpassWork(final JPasswordField jpass) {
        CaretListener caret = new CaretListener() {

            @Override
            public void caretUpdate(CaretEvent e) {
                int passwordLength = jpass.getText().length();
                if (passwordLength < 6) {
                    jLabel4.setText(null);
                    jpass.setEditable(true);
                    validate();
                } else {
                    jpass.setEditable(false);
                    validate();
                }
            }
        };

        KeyListener key = new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent event) {
                if (event.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                    jpass.setEditable(true);
                };
            }
        };
        jpass.addCaretListener(caret);
        jpass.addKeyListener(key);
    }

    public void changePass(CreditCard creditCard) {
        this.creditCard = creditCard;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLblPassNew = new javax.swing.JLabel();
        jLblPassOld = new javax.swing.JLabel();
        jLblPassNew1 = new javax.swing.JLabel();
        jPasswordFieldOdl = new javax.swing.JPasswordField();
        jPasswordFieldNew = new javax.swing.JPasswordField();
        jPasswordFieldNew1 = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        jBtnOk = new javax.swing.JButton();
        jBtnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setMaximumSize(new java.awt.Dimension(0, 0));

        jLblPassNew.setText("Password mới : ");

        jLblPassOld.setText("Password cũ : ");

        jLblPassNew1.setText("Nhập lại Password mới: ");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLblPassOld, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLblPassNew1)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jLblPassNew, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordFieldNew1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPasswordFieldNew, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPasswordFieldOdl, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldOdl, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLblPassOld, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldNew, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLblPassNew, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLblPassNew1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordFieldNew1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jBtnOk.setText("Đồng ý");
        jBtnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnOkActionPerformed(evt);
            }
        });

        jBtnCancel.setText("Hủy bỏ");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(jBtnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnOk)
                    .addComponent(jBtnCancel))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnOkActionPerformed
        // TODO add your handling code here:
        String passold = jPasswordFieldOdl.getText().toString();
        if (passold.length() != 6) {
            jLabel4.setText("Mật khẩu phải đủ 6 ký tự!");
            validate();
            return;
        }
        if (connectManeger.login(creditCard.getAccountCode(), passold)) {
            String passnew1 = jPasswordFieldNew.getText().toString();
            String passnew2 = jPasswordFieldNew1.getText().toString();
            if (passnew1.length() != 6) {
                jLabel4.setText("Mật khẩu mới phải đủ 6 ký tự!");
                validate();
                return;
            }
            if (passnew2.length() != 6) {
                jLabel4.setText("Mật khẩu mới phải đủ 6 ký tự!");
                validate();
                return;
            }
            if (passnew1.equals(passnew2)) {
                if (passnew1.equals(passold)) {
                    jLabel4.setText("Mật khẩu mới trùng mật khẩu cũ!");
                    validate();
                    return;
                } else {
                    ResultReponse reponse = connectManeger.changePassCreditCard(creditCard.getAccountCode(), passnew1);
                    if(reponse.isSuccess())
                    {
                         JOptionPane.showMessageDialog(this,
                                    reponse.getNote(),
                                    "Đổi mật khẩu thành công!",
                                    JOptionPane.INFORMATION_MESSAGE);
                         this.dispose();
                    }else
                    {
                        JOptionPane.showMessageDialog(this,
                                    reponse.getNote(),
                                    "Đổi mật khẩu thất bại!",
                                    JOptionPane.ERROR_MESSAGE);
                         this.dispose();
                    }
                }
            } else {
                jLabel4.setText("Mật khẩu mới không trùng nhau!");
                validate();
                return;
            }
        } else {
            jLabel4.setText("Mật khẩu cũ nhập sai!");
            validate();
            return;
        }
    }//GEN-LAST:event_jBtnOkActionPerformed

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBtnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChangePassWord.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChangePassWord.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChangePassWord.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChangePassWord.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ChangePassWord().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnOk;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLblPassNew;
    private javax.swing.JLabel jLblPassNew1;
    private javax.swing.JLabel jLblPassOld;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordFieldNew;
    private javax.swing.JPasswordField jPasswordFieldNew1;
    private javax.swing.JPasswordField jPasswordFieldOdl;
    // End of variables declaration//GEN-END:variables
}
